home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / cc / fixproto < prev    next >
Text File  |  1993-12-09  |  14KB  |  403 lines

  1. #!/bin/sh
  2. #
  3. # SYNOPSIS
  4. #    fixproto TARGET-DIR SOURCE-DIR-ALL ANSI POSIX [ SOURCE-DIR-STD ]
  5. #
  6. # COPYRIGHT
  7. #    Copyright (C) 1993 Free Software Foundation, Inc.
  8. #    This file is part of GNU CC.
  9. #
  10. #    GNU CC is free software; you can redistribute it and/or modify
  11. #    it under the terms of the GNU General Public License as published by
  12. #    the Free Software Foundation; either version 2, or (at your option)
  13. #    any later version.
  14. #
  15. #    GNU CC is distributed in the hope that it will be useful,
  16. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. #    GNU General Public License for more details.
  19. #
  20. #    You should have received a copy of the GNU General Public License
  21. #    along with GNU CC; see the file COPYING.  If not, write to
  22. #    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23. #
  24. # DESCRIPTION
  25. #    Adjunct script for GNU CC to populate a directory with ANSI,
  26. #    Posix.1, and C++ compatible header files.
  27. #
  28. #    Each file found under SOURCE-DIR-ALL is analyzed and "fixed."
  29. #       Only standard ANSI/POSIX files found under SOURCE-DIR-STD
  30. #       are analyzed and "fixed."
  31. #    The SOURCE-DIRs are searched in order; a file found
  32. #    under multiple SOURCE-DIRs is only handled for the first one.
  33. #
  34. #       If ANSI and POSIX are given, ANSI and POSIX required system
  35. #       headers are looked for in those subdirectories.  These should
  36. #       normally kust be given as ".", meaning that they can be found 
  37. #       in the source directory directly.
  38. #
  39. # STRATEGY
  40. #       Each include file is fed through cpp, and the scan-decls program
  41. #    parses it, and emits any found function declarations.
  42. #    The fix-header program analyzes the scan-decls output,
  43. #    together with the original include file, and writes a "fixed"
  44. #    include file, if needed.
  45. #
  46. #    The comment at the beginning of fix-header.c lists specifically
  47. #    what kind of changes are made.
  48. #
  49. # NOTE
  50. #    Some file space will be wasted, because the original header
  51. #    files are copied.  An earlier version just included the original
  52. #    by "reference", using GNU cpp's #include_next mechanism.
  53. #    This is currently not done, partly because #include_next is
  54. #    fragile (susceptible to version incompatibilties, and depends
  55. #    and GCC-specific features), and partly for performance reasons.
  56. #
  57. # AUTHORS
  58. #    Ron Guilmette (rfg@netcom.com) (original idea and code)
  59. #    Per Bothner (bothner@cygnus.com) (major re-write)
  60.  
  61. progname=$0
  62. progname=`basename $progname`
  63. original_dir=`pwd`
  64. CPP=${CPP-./cpp}
  65.  
  66. if [ `echo $1 | wc -w` = 0 ] ; then
  67.   echo $progname\: usage\: $progname target-dir \[ source-dir \.\.\. \]
  68.   exit 1
  69. fi
  70.  
  71. ansi_files="ctype.h errno.h math.h setjmp.h signal.h stdio.h stdlib.h string.h time.h"
  72. posix_files="dirent.h curses.h fcntl.h grp.h locale.h pwd.h sys/stat.h sys/times.h sys/resource.h sys/utsname.h sys/wait.h tar.h termios.h unistd.h"
  73.  
  74. rel_target_dir=$1; shift
  75.  
  76. # All files in $src_dir_all (normally same as $rel_target_dir) are
  77. # processed.
  78. src_dir_all=$1; shift
  79.  
  80. # Get ansi postfix
  81. ansi_prefix=$1; shift
  82.  
  83. posix_prefix=$1 shift;
  84.  
  85. std_files=`for f in $ansi_files; do echo ${ansi_prefix}/$f; done; \
  86.        for f in $posix_files; do echo ${posix_prefix}/$f; done`
  87.  
  88.  
  89. # In $src_dir_std (normally same as /usr/include), only the
  90. # "standard" ANSI/POSIX files listed in $std_files are processed.
  91. src_dir_std=$*
  92.  
  93. if [ `expr $rel_target_dir : '\(.\)'` != '/' ] ; then
  94.   abs_target_dir=$original_dir/$rel_target_dir
  95. else
  96.   abs_target_dir=$rel_target_dir
  97. fi
  98.  
  99. # Determine whether this system has symbolic links.
  100. if ln -s X $rel_target_dir/ShouldNotExist 2>/dev/null; then
  101.   rm -f $rel_target_dir/ShouldNotExist
  102.   LINKS=true
  103. elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then
  104.   rm -f /tmp/ShouldNotExist
  105.   LINKS=true
  106. else
  107.   LINKS=false
  108. fi
  109.  
  110. if [ \! -d $abs_target_dir ] ; then
  111.   echo $progname\: creating directory $rel_target_dir
  112.   mkdir $abs_target_dir
  113. fi
  114.  
  115. echo $progname\: populating \`$rel_target_dir\'
  116.  
  117. include_path=""
  118.  
  119. if [ `echo $* | wc -w` != 0 ] ; then
  120.   for rel_source_dir in $src_dir_all $src_dir_std; do
  121.     if [ `expr $rel_source_dir : '\(.\)'` != '/' ] ; then
  122.       abs_source_dir=$original_dir/$rel_source_dir
  123.     else
  124.       abs_source_dir=$rel_source_dir
  125.     fi
  126.     include_path="$include_path -I$abs_source_dir"
  127.   done
  128. fi
  129.  
  130. required_stdlib_h="abort abs atexit atof atoi atol bsearch calloc exit free getenv labs malloc qsort rand realloc srand strtod strtol strtoul system"
  131. # "div ldiv", - ignored because these depend on div_t, ldiv_t
  132. # ignore these: "mblen mbstowcs mbstowc wcstombs wctomb"
  133. # Should perhaps also add NULL
  134. required_unistd_h="_exit access alarm chdir chown close ctermid cuserid dup dup2 execl execle execlp execv execve execvp fork fpathconf getcwd getegid geteuid getgid getgroups getlogin getpgrp getpid getppid getuid isatty link lseek pathconf pause pipe read rmdir setgid setpgid setsid setuid sleep sysconf tcgetpgrp tcsetpgrp ttyname unlink write"
  135.  
  136. done_dirs=""
  137. echo "" >fixproto.list
  138.  
  139. for code in ALL STD ; do
  140.  
  141.   subdirs="."
  142.  
  143.   case $code in
  144.     ALL)
  145.       rel_source_dir=$src_dir_all
  146.  
  147.       dirs="."
  148.       levels=2
  149.       while $LINKS && test -n "$dirs" -a $levels -gt 0
  150.       do
  151.         levels=`expr $levels - 1`
  152.     newdirs=
  153.     for d in $dirs ; do
  154.       # Find all directories under $d, relative to $d, excluding $d itself.
  155.       # Assume directory names ending in CC or containing ++ are
  156.       # for C++, so skip those.
  157.       subdirs="$subdirs "`cd $rel_source_dir/$d; find . -type d -print | \
  158.                sed -e '/^\.$/d' -e "s|^\./|${d}/|" -e 's|^\./||' \
  159.              -e '/CC$/d' -e '/\+\+/d'`
  160.       links=
  161.       links=`cd $rel_source_dir; find $d/. -type l -print | \
  162.                sed -e "s|$d/./|$d/|" -e 's|^\./||'`
  163.       for link in $links --dummy-- ; do
  164.         test -d $rel_source_dir/$link/. && newdirs="$newdirs $link"
  165.       done
  166.     done
  167.     dirs="$newdirs"
  168.     subdirs="$subdirs $newdirs"
  169.       done
  170.       ;;
  171.     STD)
  172.     rel_source_dir=$src_dir_std
  173.       ;;
  174.   esac
  175.  
  176.   case $rel_source_dir in
  177.     /*)
  178.           abs_source_dir=$rel_source_dir
  179.       ;;
  180.     *) 
  181.       abs_source_dir=$original_dir/$rel_source_dir
  182.       ;;
  183.   esac
  184.  
  185.   if [ -d $abs_source_dir ] ; then true; else \
  186.     echo $progname\: warning\: no such directory\: \`$abs_source_dir\'
  187.     continue
  188.   fi
  189.  
  190.   for rel_source_subdir in $subdirs; do
  191.  
  192.       abs_target_subdir=${abs_target_dir}/${rel_source_subdir}
  193.       if [ \! -d $abs_target_subdir ] ; then
  194.         mkdir $abs_target_subdir
  195.       fi
  196.       # Append "/"; remove initial "./". Hence "." -> "" and "sys" -> "sys/".
  197.       rel_source_prefix=`echo $rel_source_subdir | sed -e 's|$|/|' -e 's|^./||'`
  198.  
  199.       case $code in
  200.     ALL)
  201.       # The 'sed' is in case the *.h matches nothing, which yields "*.h"
  202.       # which would then get re-globbed in the current directory.  Sigh.
  203.       rel_source_files=`cd ${abs_source_dir}/${rel_source_subdir}; echo *.h | sed -e 's|[*].h|NONE|'`
  204.       ;;
  205.  
  206.     STD)
  207.       files_to_check="$std_files"
  208.       rel_source_files=""
  209.  
  210.       # Also process files #included by the $std_files.
  211.       while [ -n "${files_to_check}" ]
  212.       do
  213.         new_files_to_check=""
  214.         for file in $files_to_check ; do
  215.           xxfile=`echo $file | sed -e 's|/\([^/\.][^/\.]*\)/\.\./|/|'`
  216.           # Create the dir where this file will go when fixed.
  217.           xxdir=`echo ./$file | sed -e 's|/[^/]*$||'`
  218.           if [ \! -d $abs_target_subdir/$xxdir ] ; then
  219.         mkdir $abs_target_subdir/$xxdir
  220.           fi
  221.           # Just in case we have edited out a symbolic link
  222.           if [ -f $src_dir_std/$file -a -f $src_dir_std/$xxfile ] ; then
  223.         file=$xxfile
  224.           fi
  225.           case " $rel_source_files " in
  226.             *" ${file} "*)
  227.           # Already seen $file; nothing to do
  228.           ;;
  229.         *)
  230.           if [ -f $src_dir_std/$file ]; then true; else 
  231.             for prefix in $ansi_prefix $posix_prefix; do
  232.             if [ -f $src_dir_std/$prefix/$file ]; then
  233.                file=$prefix/$file;
  234.             fi
  235.             done
  236.           fi
  237.  
  238.           if test -f $src_dir_std/$file ; then
  239.             rel_dir=`echo $file | sed -n -e 's|^\(.*/\)[^/]*$|\1|p'`
  240.             # For #include "foo.h", that might be either "foo.h"
  241.             # or "${rel_dir}foo.h (or something bogus).
  242.                 new_files_to_check="$new_files_to_check "`sed -n \
  243.             -e 's@    @ @g' \
  244.                 -e 's@^ *# *include *<\([^>]*\)>.*$@\1@p' -e \
  245.                 's@^ *# *include *\"\([^\"]*\)\".*$@\1 '$rel_dir'\1@p'\
  246.             <$src_dir_std/$file`
  247.                 rel_source_files="$rel_source_files $file"
  248.           fi
  249.           ;;
  250.           esac
  251.         done
  252.         files_to_check="$new_files_to_check"
  253.       done
  254.       rel_source_files="$rel_source_files"
  255.       ;;
  256.       esac
  257.  
  258.       for filename in $rel_source_files ; do
  259.     rel_source_file=${rel_source_prefix}${filename}
  260.     abs_source_file=$abs_source_dir/$rel_source_file
  261.     abs_target_file=$abs_target_dir/$rel_source_file
  262.  
  263.     if [ -f $abs_source_file ]; then true; else
  264.       for prefix in $ansi_prefix $posix_prefix; do
  265.       if [ -f $abs_source_dir/$prefix/$rel_source_file ]; then
  266.         rel_source_file=$prefix/$rel_source_file
  267.         abs_source_file=$abs_source_dir/$rel_source_file
  268.         abs_target_file=$abs_target_dir/$rel_source_file
  269.       fi
  270.      done
  271.     fi    
  272.  
  273.     if test "$filename" = 'NONE' ; then
  274.       echo "(No *.h files in $abs_source_dir/$rel_source_subdir)"
  275.     # If target file exists, check if was written while processing one
  276.     # of the earlier source directories;  if so ignore it.
  277.     elif test -f $abs_target_file \ 
  278.       && grep "$rel_source_file" fixproto.list >/dev/null
  279.     then true
  280.     else
  281.       # echo doing $rel_source_file from $abs_source_dir
  282.       required_list=
  283.       extra_check_list=
  284.       case $rel_source_file in
  285.         *ctype.h)
  286.           required_list="isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit tolower toupper" ;;
  287.         *dirent.h)
  288.           required_list="closedir opendir readdir rewinddir" ;;
  289.         *errno.h)
  290.           extra_check_list="errno" ;;
  291.         *curses.h)
  292.           required_list="box delwin endwin getcurx getcury initscr mvcur mvwprintw mvwscanw newwin overlay overwrite scroll subwin touchwin waddstr wclear wclrtobot wclrtoeol waddch wdelch wdeleteln werase wgetch wgetstr winsch winsertln wmove wprintw wrefresh wscanw wstandend wstandout" ;;
  293.         *fcntl.h)
  294.           required_list="creat fcntl open" ;;
  295.         *grp.h)
  296.           #Maybe also "getgrent fgetgrent setgrent endgrent" */
  297.           required_list="getgrgid getgrnam" ;;
  298.         *limit.h)
  299.           required_list= /* Lots of macros */ ;;
  300.         *locale.h)
  301.           required_list="localeconv setlocale" ;;
  302.         *math.h)
  303.           required_list="acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp ldexp log10 log modf pow sin sinh sqrt tan tanh"
  304.           extra_check_list="HUGE_VAL" ;;
  305.         *pwd.h)
  306.           required_list="getpwnam getpwuid" ;;
  307.         *setjmp.h)
  308.           # Left out siglongjmp sigsetjmp - these depend on sigjmp_buf.
  309.           required_list="longjmp setjmp" ;;
  310.         *signal.h)
  311.           # Left out signal() - its prototype is too complex for us!
  312.           # Also left out "sigaction sigaddset sigdelset sigemptyset
  313.           # sigfillset sigismember sigpending sigprocmask sigsuspend"
  314.           # because these need sigset_t or struct sigaction.
  315.           # Most systems that provide them will also declare them.
  316.           required_list="kill raise" ;;
  317.         stdio.h)
  318.           required_list="clearerr fclose feof ferror fflush fgetc fgetpos fgets fopen fprintf fputc fputs fread freopen fscanf fseek fsetpos ftell fwrite getc getchar gets perror printf putc putchar puts remove rename rewind scanf setbuf setvbuf sprintf sscanf vprintf vsprintf vfprintf tmpfile tmpnam ungetc"
  319.           if grep '[^_a-zA-Z0-9]_flsbuf' <$abs_source_file >/dev/null; then
  320.         required_list="$required_list _flsbuf _filbuf"
  321.           fi
  322.           # Should perhaps also handle NULL, EOF, ... ?
  323.           ;;
  324.         *stdlib.h)
  325.           required_list="$required_stdlib_h" ;;
  326.         *string.h)
  327.           required_list="memchr memcmp memcpy memmove memset strcat strchr strcmp strcoll strcpy strcspn strerror strlen strncat strncmp" ;;
  328. # Should perhaps also add NULL and size_t
  329.         *sys/stat.h)
  330.           required_list="chmod fstat mkdir mkfifo stat umask"
  331.           extra_check_list="S_ISDIR S_ISBLK S_ISCHR S_ISFIFO S_ISREG S_ISLNK S_IFDIR S_IFBLK S_IFCHR S_IFIFO S_IFREG S_IFLNK" ;;
  332.         *sys/times.h)
  333.           required_list="times" ;;
  334. # "sys/types.h" add types (not in old g++-include)
  335.         *sys/utsname.h)
  336.           required_list="uname" ;;
  337.         *sys/wait.h)
  338.           required_list="wait waitpid"
  339.           extra_check_list="WEXITSTATUS WIFEXITED WIFSIGNALED WIFSTOPPED WSTOPSIG WTERMSIG WNOHANG WNOTRACED" ;;
  340.         *tar.h)
  341.           required_list=  ;;
  342.         *termios.h)
  343.           required_list="cfgetispeed cfgetospeed cfsetispeed cfsetospeed tcdrain tcflow tcflush tcgetattr tcsendbreak tcsetattr" ;;
  344.         *time.h)
  345.           required_list="asctime clock ctime difftime gmtime localtime mktime strftime time tzset" ;;
  346.         *unistd.h)
  347.           required_list="$required_unistd_h" ;;
  348.       esac
  349.       rm -f fixtmp.c fixtmp.i
  350.       echo "#include <${rel_source_file}>" >fixtmp.c
  351.       for macro in ${required_list} ${extra_check_list}
  352.       do
  353.         echo "#ifdef ${macro}" >>fixtmp.c
  354.         echo "__DEFINED_MACRO_${macro};" >>fixtmp.c
  355.         echo "#endif" >>fixtmp.c
  356.       done
  357.       if ${CPP} -D__STDC__ -D__cplusplus -D_POSIX_SOURCE $include_path fixtmp.c >fixtmp.i 2>/dev/null
  358.       then
  359.        $original_dir/fix-header $rel_source_file $abs_source_file $abs_target_file "$required_list" <fixtmp.i
  360.       else
  361.         echo "${progname}: cpp could not parse ${abs_source_file} (ignored)"
  362.       fi
  363.       echo "${rel_source_file}" >>fixproto.list
  364.     fi
  365.       done
  366.       rm -f fixtmp.c fixtmp.i
  367.     done
  368.     # check for broken assert.h that needs stdio.h
  369.     if test -f $abs_source_dir/assert.h -a \! -f $abs_target_dir/assert.h \
  370.       && grep 'stderr' $abs_source_dir/assert.h >/dev/null
  371.     then
  372.       if grep 'include.*stdio.h' $abs_source_dir/assert.h >/dev/null
  373.       then true
  374.       else
  375.     echo 'Fixing broken assert.h (needs stdio.h)'
  376.     cat $abs_source_dir/assert.h >$abs_target_dir/assert.h
  377.     echo '#include <stdio.h>' >>$abs_target_dir/assert.h
  378.       fi
  379.     fi
  380.     done_dirs="$done_dir $rel_source_dir"
  381. done
  382.  
  383. # This might be more cleanly moved into the main loop, by adding
  384. # a <dummy> source directory at the end.  FIXME!
  385. for rel_source_file in unistd.h stdlib.h
  386. do
  387.   if grep "$rel_source_file" fixproto.list >/dev/null
  388.   then true
  389.   else
  390.     echo Adding missing $rel_source_file
  391.     rel_source_ident=`echo $rel_source_file | tr ./ __`
  392.     required_list=`eval echo '${required_'${rel_source_ident}'-}'`
  393.     cat >tmp.h <<EOF
  394. #ifndef ${rel_source_ident}
  395. #define ${rel_source_ident}
  396. #endif
  397. EOF
  398.     $original_dir/fix-header $rel_source_file tmp.h $abs_target_dir/$rel_source_file "$required_list" </dev/null
  399.     rm tmp.h
  400.   fi
  401. done
  402. exit 0
  403.